package com.sqx.modules.resumes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.postPush.entity.PostPush;
import com.sqx.modules.postPush.service.PostPushService;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.dao.ResumesDao;
import com.sqx.modules.resumes.service.IResumesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wuchang
 * @since 2023-07-24
 */
@Service
public class ResumesServiceImpl extends ServiceImpl<ResumesDao, Resumes> implements IResumesService {

    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private PostPushService postPushService;
    @Autowired
    private UserService userService;

    @Override
    public Result saveResumes(Resumes resumes) {
        if (resumes.getUserId()==null){
            return Result.error("用户id不能为空");
        }
        QueryWrapper<Resumes> wrapper = new QueryWrapper<Resumes>().eq("user_id", resumes.getUserId());
        if (resumes.getResumesId() != null) {
            wrapper.ne("resumes_id", resumes.getResumesId());
        }
        CommonInfo maxResumesCount = commonInfoService.findOne(343);
        Integer count = baseMapper.selectCount(wrapper);
        if (count >= Integer.parseInt(maxResumesCount.getValue())) {
            return Result.error("简历数量最多不能超过" + maxResumesCount.getValue() + "个");
        }
        if (resumes.getResumesId() != null) {
            resumes.setUpdateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.updateById(resumes));
        } else {
            resumes.setCreateTime(LocalDateTime.now());
            resumes.setUpdateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.insert(resumes));
        }
    }

    @Override
    public IPage<Resumes> getMyResumesList(Integer page, Integer limit, Resumes resumes) {
        Page<Resumes> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<Resumes> selectPage = baseMapper.selectPage(pages, new QueryWrapper<>(resumes).orderByDesc("create_time"));
        for (Resumes record : selectPage.getRecords()) {
            record.setUserEntity(userService.getById(record.getUserId()));
        }
        return selectPage;

    }

    @Override
    public Result deleteResumes(Long userId, Long resumesId) {
        Resumes resumes = baseMapper.selectById(resumesId);
        if (resumes == null) {
            return Result.error("当前简历不存在");
        }
        if (!resumes.getUserId().equals(userId)) {
            return Result.error("您无权操作");
        } else {
            int count = postPushService.count(new QueryWrapper<PostPush>().eq("resumes_id", resumesId));
            if (count > 0) {
                return Result.error("当前简历已关联求职信息,请先删除后再重试!");
            } else {
                return Result.upStatus(baseMapper.deleteById(resumesId));
            }
        }

    }

    @Override
    public Resumes getResumesInfo(Long postPushId) {
        PostPush postPush = postPushService.getById(postPushId);
        if (postPush.getResumesId() == null) {
            return null;
        }
        Resumes resumes = baseMapper.selectById(postPush.getResumesId());
        resumes.setUserEntity(userService.getById(resumes.getUserId()));
        return resumes;


    }
}
